<template>
  <div class="auth">
    <div class="main">
      <div style="background:#F3F7F8;padding:0 25px;">
        <div style="text-align:center">
          <img src="/static/images/logo.png" alt />
        </div>
        <p>为了给您提供完整功能需要获取以下权限</p>
        <p class="userInfo">·获得你的公开信息（昵称、头像等）</p>
      </div>
      <div style="padding:0 25px;">
        <button
          class="btn"
          open-type="getUserInfo"
          @getuserinfo="getUserInfo"
          type="default"
          hover-class="none"
        >点击登录</button>
        <!-- <div class="btn">确定授权</div> -->
      </div>
    </div>
    <div class="bottom"></div>
  </div>
</template>

<script>
import { apiWechatAuthMini } from "@/api/api";
// import { mapGetters } from 'vuex';
export default {
  data() {
    return {
      res: null
    };
  },
  // computed: {
  //   ...mapGetters(['userInfo'])
  // },
  // mounted () {
  //   // console.log(this.userInfo, 'raeeeeeee');
  // },
  mounted() {
    Object.assign(this.$data, this.$options.data());
    if (wx.getStorageSync("userInfo")) {
      // eslint-disable-next-line no-undef
      let pages = getCurrentPages();
      let options = pages[pages.length - 2].options;
      console.log(options, "optionsoptionsoptions");
      let url = "";
      let urlType = false;
      if (JSON.stringify(options) !== "{}") {
        urlType = true;
        // eslint-disable-next-line no-unused-vars
        for (let a in options) {
          console.log(a, "rrrrrrrrrr");
          url += `&${a}=${options[a]}`;
        }
        var newUrl = url.replace("&", "?");
      }

      let routeUrl = pages[pages.length - 2].route + (urlType ? newUrl : "");
      var arr = [
        "pages/store/store/main",
        "pages/index/index/main",
        "pages/PersonalCenter/personal/main",
        "pages/moneyMan/moneyMan/main",
        "pages/money/money/main"
      ];
      var res = arr.find(function(elem) {
        if (elem === routeUrl) {
          return true;
        } else {
          return false;
        }
      });
      if (res) {
        wx.reLaunch({
          url: `/${routeUrl}`
        });
      } else {
        wx.navigateTo({
          url: `/${routeUrl}`
        });
      }
      // this.$store.commit('userFun', routeUrl);
    }
  },
  methods: {
    // 获取微信授权
    getUserInfo(e) {
      // eslint-disable-next-line no-undef
      let pages = getCurrentPages();
      let options = pages[pages.length - 2].options;
      let url = "";
      let urlType = false;
      if (JSON.stringify(options) !== "{}") {
        urlType = true;
        // eslint-disable-next-line no-unused-vars
        for (let a in options) {
          url += `&${a}=${options[a]}`;
        }
        var newUrl = url.replace("&", "?");
      }
      let routeUrl = pages[pages.length - 2].route + (urlType ? newUrl : "");

      const that = this;
      let userInfo = e.target ? e.target.userInfo : null;
      if (!userInfo) {
        wx.showToast({
          title: "拒绝授权，将无法使用本小程序全部功能，请重新获取授权！",
          icon: "none",
          mask: true
        });
        return;
      }
      wx.login({
        success(result) {
          if (result.code) {
            wx.showLoading({ title: "授权中...", mask: "true" });
            wx.getUserInfo({
              success(res) {
                const data = {
                  headImg: res.userInfo.avatarUrl,
                  nickName: res.userInfo.nickName,
                  code: result.code
                };
                apiWechatAuthMini(data)
                  .then(item => {
                    wx.hideLoading();
                    if (item.code) {
                      wx.setStorageSync("token", JSON.parse(item.msg).token);
                      if (JSON.parse(item.msg).toMp) {
                        that.$store.commit("userFun", null);
                        wx.redirectTo({
                          url: `/pages/webView/main`
                        });
                      } else {
                        that.$store.commit("userFun", routeUrl);
                      }
                    }
                  })
                  .catch(msg => {
                    // console.log(msg)
                  });
              },
              // 获取失败，弹窗提示一键登录
              fail() {
                wx.hideLoading();
              }
            });
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
.auth {
  .bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 376px;
    width: 100%;
    background: url("https://qinghe-zichan.oss-cn-shanghai.aliyuncs.com/imgs/authBg.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }
  .main {
    font-size: 16px;
    color: #020604;
    .btn {
      z-index: 99;
      height: 46px;
      background: rgba(24, 119, 152, 1);
      border-radius: 2px;
      font-size: 15px;
      color: #fff;
      text-align: center;
      line-height: 46px;
    }
    img {
      width: 169px;
      height: 68px;
      padding-top: 70px;
      margin-bottom: 62px;
    }
    .userInfo {
      font-size: 13px;
      color: #6b6b6b;
      padding-top: 10px;
      padding-bottom: 53px;
    }
  }
}
</style>